package com.yanqu.road.server.cmd.union;

import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.pb.UnionPb;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.S_CROSS_UNION_QUERY, desc = "查看跨服联盟")
public class QueryCrossUnionCmd extends AbstractCommand {

	private long serverId;

	private String unionUid;

	private CrossUnionInfo unionInfo;

	private UnionProto.CrossUnionQueryReqMsg reqMsg;

	private boolean respFlag = false;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = UnionProto.CrossUnionQueryReqMsg.parseFrom(packet.getBytes());
		serverId = reqMsg.getServerId();
		unionUid = reqMsg.getUnionUid();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(serverId == GameServer.getInstance().getServerId()){
			respFlag = true;
			unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
			if(null == unionInfo){
				ret = GameErrorCode.E_UNION_NO_FOUND;
			}
		}else{
			ServerInfo serverInfo = ServerListMgr.getServerInfo(reqMsg.getServerId());
			if(serverInfo == null || serverInfo.getChannelId() != ServerListMgr.getChannelId(GameServer.getInstance().getServerId())){
				UnionProto.CrossUnionQueryReqMsg.Builder gogogogo = reqMsg.toBuilder();
				gogogogo.setReqServerId(GameServer.getInstance().getServerId());
				gogogogo.setReqChannelId(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()));
				//没找到就是跨域的，直接上跨域
				player.sendChannelPlayerPacket(ChannelProtocol.N_CHANNEL_QUERY_UNION, gogogogo);
			}else{
				YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_QUERY_UNION, reqMsg);
				GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
			}
		}

	}

	@Override
	public void sendData(GamePlayer player) {
		if(respFlag){
			UnionProto.CrossUnionQueryRespMsg.Builder respMsg = UnionProto.CrossUnionQueryRespMsg.newBuilder();
			respMsg.setRet(ret);
			if(0 == ret){
				respMsg.setServerId(serverId);
				respMsg.setUnion(UnionPb.parseUnionTempMsg(unionInfo));
				int crossServerCount = CrossUnionServerGroupMgr.getGroupServerCountByServerId(unionInfo.getServerId());
				int unionMaxMemberCount = UnionConfigMgr.getMaxMemberCount(unionInfo.getExperience(), unionInfo.isCross(), crossServerCount);
				respMsg.setMaxMemberCount(unionMaxMemberCount);
				Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionInfo.getUnionUid());
				if (null != memberMap) {
					for (UnionMember member : memberMap.values()) {
						UnionProto.UnionMemberTempMsg.Builder memberMsg = UnionPb.parseUnionMemberTempMsg(member);
						if(null != memberMsg) {
							respMsg.addMemberList(memberMsg);
						}
					}
				}
			}
			player.sendPacket(Protocol.U_CROSS_UNION_QUERY, respMsg);
		}
	}

}
